<template>
  <div class="login-container">
    <div class="login-form">
      <h2>外卖平台登录</h2>
      <div class="form-group">
        <label for="username">用户名</label>
        <input 
          type="text" 
          id="username" 
          v-model="username" 
          placeholder="请输入用户名"
          required
        />
      </div>
      <div class="form-group">
        <label for="password">密码</label>
        <input 
          type="password" 
          id="password" 
          v-model="password" 
          placeholder="请输入密码"
          required
        />
      </div>
      <button class="login-btn" @click="handleLogin">登录</button>
      <div class="register-link">
        还没有账号？<router-link to="/register">立即注册</router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { userAPI } from '../api/api.js';

export default {
  name: 'Login',
  data() {
    return {
      username: '',
      password: '',
      loading: false // 添加加载状态
    }
  },
  methods: {
    // 处理登录
    async handleLogin() {
      if (!this.username || !this.password) {
        alert('请输入用户名和密码');
        return;
      }

      this.loading = true;
      try {
        // 调用真实后端登录API
        const response = await userAPI.login(this.username, this.password);
        console.log('Login response:', response);
        
        // 保存登录状态和token
        if (response && response.token) {
          const userData = {
            id: response.id,
            username: this.username,
            token: response.token
          };
          
          localStorage.setItem('user', JSON.stringify(userData));
          localStorage.setItem('token', response.token);
          localStorage.setItem('isLoggedIn', 'true'); // 添加isLoggedIn标志以保持兼容性
          
          // 登录成功后直接跳转到点餐页面，不显示弹窗
          this.$router.push('/order');
        } else {
          alert('登录失败，请检查用户名和密码');
        }
      } catch (error) {
        console.error('Login error:', error);
        alert('登录失败: ' + (error.message || '请稍后重试'));
      } finally {
        this.loading = false;
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.login-form {
  background-color: white;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.login-form h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #666;
  font-size: 14px;
}

.form-group input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  transition: border-color 0.3s;
}

.form-group input:focus {
  outline: none;
  border-color: #1890ff;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background-color: #1890ff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.login-btn:hover {
  background-color: #40a9ff;
}

.register-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: #666;
}

.register-link a {
  color: #1890ff;
  text-decoration: none;
}

.register-link a:hover {
  text-decoration: underline;
}
</style>